//
//  TFSFile.swift
//  TFSClient
//
//  Created by Samuel Richard Magny on 2015-09-07.
//  Copyright (c) 2015 Samuel Richard Magny. All rights reserved.
//

import Cocoa
import ObjectiveGit

public class TFSFile: NSObject {
    public enum FileStatus: Int, CustomStringConvertible {
        
        case Unmodified
        case Added
        case Removed
        case Modified
        case Renamed
        case Copied
        case Ignored
        case Untracked
        case TypeChange
        case Conflicted
        case UpToDate
        
        public var description : String {
            switch self {
                // Use Internationalization, as appropriate.
            case .Unmodified: return "Unmodified";
            case .Added: return "Added";
            case .Removed: return "Removed";
            case .Modified: return "Modified";
            case .Renamed: return "Renamed";
            case .Copied: return "Copied";
            case .Ignored: return "Ignored";
            case .Untracked: return "Untracked";
            case .TypeChange: return "TypeChange";
            case .Conflicted: return "Conflicted";
            case .UpToDate: return "Up To Date";
            }
        }
    }
    
    public var status:FileStatus
    public var filePath:String
    public var oldFilePath:String
    public var staged:Bool = false
    
    internal var indexRefOID:AnyObject?
    
    public var fileName:String {
        get{
            return NSURL(fileURLWithPath: filePath).lastPathComponent!
        }
    }
    
    public var oldFileName:String {
        get{
            return NSURL(fileURLWithPath: oldFilePath).lastPathComponent!
        }
    }
    
    public override init()
    {
        self.status = FileStatus.Unmodified
        self.filePath = ""
        self.oldFilePath = ""
    }
    
    public init (status:FileStatus, filePath:String, oldFilePath:String = "")
    {
        self.status = status
        self.filePath = filePath
        self.oldFilePath = oldFilePath
    }
    
    internal convenience init (status:GTStatusDeltaStatus, filePath:String, oldFilePath:String = "")
    {
        let statusTFS:TFSFile.FileStatus = TFSFile.convertObjectiveGitFileStatus(status)
        self.init(status:statusTFS, filePath:filePath, oldFilePath:oldFilePath)
    }
    
    internal convenience init (entryStatus:GTIndexEntryStatus, filePath:String, oldFilePath:String = "")
    {
        let statusTFS:TFSFile.FileStatus = TFSFile.convertObjectiveGitEntryStatus(entryStatus)
        self.init(status:statusTFS, filePath:filePath, oldFilePath:oldFilePath)
    }
    
    public func copyProperties(file:TFSFile)
    {
        self.filePath = file.filePath
        self.staged = file.staged
        self.status = file.status
    }
    
    private static func convertObjectiveGitFileStatus(objStatus:GTStatusDeltaStatus)->TFSFile.FileStatus
    {
        switch (objStatus)
        {
        case GTStatusDeltaStatus.Unmodified:
            return TFSFile.FileStatus.Unmodified
        case GTStatusDeltaStatus.Added:
            return TFSFile.FileStatus.Added
        case GTStatusDeltaStatus.Deleted:
            return TFSFile.FileStatus.Removed
        case GTStatusDeltaStatus.Modified:
            return TFSFile.FileStatus.Modified
        case GTStatusDeltaStatus.Renamed:
            return TFSFile.FileStatus.Renamed
        case GTStatusDeltaStatus.Copied:
            return TFSFile.FileStatus.Copied
        case GTStatusDeltaStatus.Ignored:
            return TFSFile.FileStatus.Ignored
        case GTStatusDeltaStatus.Untracked:
            return TFSFile.FileStatus.Untracked
        case GTStatusDeltaStatus.TypeChange:
            return TFSFile.FileStatus.TypeChange
        }
    }
    
    private static func convertObjectiveGitEntryStatus(objStatus:GTIndexEntryStatus)->TFSFile.FileStatus
    {
        switch (objStatus)
        {
        case GTIndexEntryStatus.Updated:
            return TFSFile.FileStatus.Modified
        case GTIndexEntryStatus.Added:
            return TFSFile.FileStatus.Added
        case GTIndexEntryStatus.Removed:
            return TFSFile.FileStatus.Removed
        case GTIndexEntryStatus.Conflicted:
            return TFSFile.FileStatus.Conflicted
        case GTIndexEntryStatus.UpToDate:
            return TFSFile.FileStatus.UpToDate
        }
    }
    
    // CustomStringConvertible
    //----------------------------------------------
    public override var description : String {
        return "Path: " + filePath +
            "\n\tStatus: " + status.description
    }
    
}
